package edu.sdwu.liqing.qasystem.controller;


import edu.sdwu.liqing.qasystem.annotation.HasRole;
import edu.sdwu.liqing.qasystem.entity.QaAnswer;
import edu.sdwu.liqing.qasystem.entity.QaAnswerComment;
import edu.sdwu.liqing.qasystem.model.Response;
import edu.sdwu.liqing.qasystem.service.IQaAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 问题回答表  前端控制器
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-04-05
 */
@RestController
@RequestMapping("api/qa-answer")
public class QaAnswerController {

    @Autowired
    IQaAnswerService answerService;

    @PostMapping
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<String> save(@RequestBody QaAnswer answer) {
        if (answerService.save(answer)) {
            return Response.ok("回答成功");
        }
        return Response.error("回答失败");
    }

    @PutMapping
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<String> update(@RequestBody QaAnswer answer) {
        if (answerService.update(answer)) {
            return Response.ok("回答更新成功");
        }
        return Response.error("回答更新失败");
    }

    @DeleteMapping("{id}")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<String> delete(@PathVariable("id") String id) {
        if (answerService.delete(id)) {
            return Response.ok("回答删除成功");
        }
        return Response.error("回答删除失败");
    }

    @GetMapping("{questionId}")
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    public Response<List<QaAnswer>> list(@PathVariable("questionId") String questionId) {
        return Response.ok(answerService.listByQuestionId(questionId));
    }
}

